package edu.cmu.cs.stage3.alice.authoringtool.viewcontroller;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringToolResources;
import edu.cmu.cs.stage3.alice.authoringtool.datatransfer.TransferableFactory;

/**
 * @author Jason Pratt
 */
public class ElementDnDPanel extends edu.cmu.cs.stage3.alice.authoringtool.util.DnDGroupingPanel implements edu.cmu.cs.stage3.alice.authoringtool.util.GUIElement, edu.cmu.cs.stage3.alice.authoringtool.util.Releasable {
	protected edu.cmu.cs.stage3.alice.core.Element element;
	protected ElementNamePropertyViewController nameViewController;
	protected javax.swing.JLabel iconLabel = new javax.swing.JLabel();

	public ElementDnDPanel() {
		setBackground( AuthoringToolResources.getColor( "elementDnDPanel" ) );
		iconLabel.setOpaque( false );
	}

	public void set( edu.cmu.cs.stage3.alice.core.Element element ) {
		clean();
		this.element = element;
		nameViewController = edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getElementNamePropertyViewController( element );
		nameViewController.setBorder( null );
		nameViewController.setOpaque( false );
		add( nameViewController, java.awt.BorderLayout.CENTER );
		addDragSourceComponent( nameViewController );
		setTransferable( TransferableFactory.createTransferable( element ) );
	}

	public void editName() {
		nameViewController.editValue();
	}

	protected void startListening() {
		if( nameViewController != null ) {
			nameViewController.startListening();
		}
	}

	protected void stopListening() {
		if( nameViewController != null ) {
			nameViewController.stopListening();
		}
	}

	public void goToSleep() {
		stopListening();
	}

	public void wakeUp() {
		startListening();
	}

	public void clean() {
		removeDragSourceComponent( nameViewController );
		setTransferable( null );
		if( nameViewController != null ) {
			remove( nameViewController );
		}
		nameViewController = null;
	}

	public void die() {
		clean();
	}

	//Rem @Override
	public void release() {
		edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.releaseGUI( this );
	}
}
